package com.ttsx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttsx.core.common.util.WebUtils;
import com.ttsx.mapper.CartMapper;
import com.ttsx.pojo.dto.cart.CartUpdateNumDTO;
import com.ttsx.pojo.entity.Cart;
import com.ttsx.pojo.entity.User;
import com.ttsx.service.CartService;
import com.ttsx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lucky
 * @description 针对表【ttsx_cart(购物车表)】的数据库操作Service实现
 * @createDate 2025-03-01 17:41:06
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart>
        implements CartService {

    @Autowired
    private ProductService productService;

    @Override
    public Long getCartCount(Long userId) {
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Cart::getUserId, userId);

        return baseMapper.selectCount(lqw);
    }

    @Override
    public List<Cart> getByUserId(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Cart::getUserId, user.getId());

        List<Cart> carts = baseMapper.selectList(lqw);
        return carts.stream()
                .peek(cart -> cart.setProduct(productService.getById(cart.getProductId())))
                .collect(Collectors.toList());
    }

    @Override
    public List<Cart> getByProductIds(List<Long> ids) {
        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
        lqw.in(Cart::getId, ids);

        List<Cart> carts = baseMapper.selectList(lqw);
        return carts.stream().peek(cart -> cart.setProduct(productService.getById(cart.getProductId())))
                .collect(Collectors.toList());
    }

    @Override
    public Boolean updateNum(CartUpdateNumDTO cartUpdateNumDTO) {
        Long cartId = cartUpdateNumDTO.getCartId();
        Integer quantity = cartUpdateNumDTO.getQuantity();

        Cart cart = new Cart();
        cart.setId(cartId);
        cart.setQuantity(quantity);

        return super.updateById(cart);
    }

    @Override
    public List<Cart> getByCartIds(List<Long> cartIds) {
        List<Cart> carts = new ArrayList<>();
        cartIds.forEach(cartId -> {
            Cart cart = this.getByCartId(cartId);
            carts.add(cart);
        });
        return carts;
    }

    @Override
    public Cart getByCartId(Long cartId) {
        Cart cart = baseMapper.selectById(cartId);
        cart.setProduct(productService.getById(cart.getProductId()));

        return cart;
    }

    @Override
    public boolean removeById(Serializable id) {
        super.removeById(id);

        HttpSession session = WebUtils.getRequest().getSession();
        User user = (User)session.getAttribute("user");
        user.setCartCount(user.getCartCount() - 1);
        session.setAttribute("user", user);

        return true;
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        super.removeBatchByIds(list);

        HttpSession session = WebUtils.getRequest().getSession();
        User user = (User)session.getAttribute("user");
        user.setCartCount(user.getCartCount() - list.size());
        session.setAttribute("user", user);

        return true;
    }

    @Override
    public Cart saveCart(Cart entity) {
        Long productId = entity.getProductId();

        if (productId != null) {
            LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Cart::getProductId, productId);
            lqw.eq(Cart::getUserId, entity.getUserId());

            Cart cart = baseMapper.selectOne(lqw);
            if (cart != null) {
                cart.setQuantity(cart.getQuantity() + entity.getQuantity());
                super.updateById(cart);

                return cart;
            }
        }

        super.save(entity);

        // 更新购物车数量
        HttpSession session = WebUtils.getRequest().getSession();
        User user = (User)session.getAttribute("user");
        user.setCartCount(user.getCartCount() + 1);
        session.setAttribute("user", user);

        return entity;
    }
}




